package com.m3958.firstgwt.server.service;

import java.io.File;
import java.io.IOException;

import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.m3958.firstgwt.server.service.SiteConfiger.SiteConfigerKeyName;

import freemarker.cache.FileTemplateLoader;
import freemarker.cache.MultiTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;


@Singleton
public class AllSitesFMCfg extends Configuration{
	
	@Inject
	public AllSitesFMCfg(DefaultObjectWrapper dow,SiteConfiger scs,DbTplLoader dbLoader) throws IOException {
		FileTemplateLoader fileLoader = null;
		Optional<String> siteroot = Optional.<String>of(scs.getValue(SiteConfigerKeyName.SITE_ROOT));
		
		fileLoader = new FileTemplateLoader(new File(siteroot.get()));
		
		TemplateLoader[] loaders = new TemplateLoader[]{fileLoader,dbLoader};
		MultiTemplateLoader mtl = new MultiTemplateLoader(loaders);
		setTemplateLoader(mtl);
		setObjectWrapper(dow);
		setLocalizedLookup(true);
		setDefaultEncoding("utf8");
	}

}
